//
//  StatusViewModel.swift
//  JLWeibo
//
//  Created by sunjinliang on 15/12/10.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

import UIKit
class StatusViewModel: CustomStringConvertible{
    var status: Status
    /// 配图URL数组
    var thumbnailArray: [NSURL]?
    init(statusM:Status){
        status = statusM
        loadthumbnail_picURL()
           }
    
    lazy var rowHeight: CGFloat = {
        let cell: StatusTableViewCell
        if self.status.retweeted_status == nil {
            
            cell = StatusNomalCell(style: .Default, reuseIdentifier: statusNormalIdentifier)
        }else{
             cell = StatusRetweetedCell(style: .Default, reuseIdentifier: statusRetweetedIdentifier)
        }
        return cell.rowHeight(self)
    }()
    /// 加载配图rul数组
    private func loadthumbnail_picURL(){
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls{
            thumbnailArray = [NSURL]()
            for dict in urls{
                thumbnailArray?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
         }

    }
    /// 转发微博文本
    var retweetedText: String? {
        guard let st = status.retweeted_status else{
            return nil
        }
        return "@" + (st.user?.screen_name ?? "") + ":" + (st.text ?? "")
    }
    /// StatusID
    var statusID : String {
        
        return (status.retweeted_status == nil) ? statusNormalIdentifier : statusRetweetedIdentifier
    }
    var userProfileUrl: NSURL{
        return NSURL(string: (status.user?.profile_image_url)!)!
    }
    var userDefaultIconView: UIImage {
        let img = UIImage(named: "avatar_default_big")
        return img!
    }
    var userMemberImage :UIImage? {
        if status.user?.mbrank>0 && status.user?.mbrank<7{
           return  UIImage(named: "common_icon_membership_level\((status.user?.mbrank)!)")
        }
        return nil
    }
    /// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    var userVipImage: UIImage?{
        switch (status.user?.verified_type ?? -1){
        case 0: return UIImage(named: "avatar_vip")
        case 2,3,4: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default :return nil
    }
    
    }
    var description: String {
        return status.description + "配图-------------\(thumbnailArray ?? [] as? NSArray)"
    }
    
}